//
//  MineItemView.swift
//  LeGou
//
//  Created by lxm on 2022/7/29.
//

import UIKit

class MineItemView: UIView {
    //MARK: --------------------属性--------------------
    var data : [(title : String, icon : String, id : Int)] {
        get {
            if AppHelper.appVerify() {
                return [
                    (title : "商城订单", icon : "mine_item_order", id : 1),
                    (title : "积分订单", icon : "mine_item_oreder_3", id : 6),
                    (title : "其他订单", icon : "mine_item_oreder_2", id : 2)
                ]
            } else {
                return [
                    (title : "商城订单", icon : "mine_item_order", id : 1),
                    (title : "积分订单", icon : "mine_item_oreder_3", id : 6),
                    (title : "其他订单", icon : "mine_item_oreder_2", id : 2),
                    (title : "创客系统", icon : "mine_item_man", id : 3),
            //        (title : "养老津贴", icon : "mine_item_old", id : 4),
                    (title : "邀请好友", icon : "mine_item_invite", id : 5)
                ]
            }
        }
    }
    
    var onClick : ((_ item : (title : String, icon : String, id : Int))->())?
    
    //MARK: --------------------UI控件--------------------
    let stack = ZMUtil.stackView(axis: .horizontal, spacing: 6)
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        for item in data {
            let itemView = ItemView()
            itemView.title.text = item.title
            itemView.icon.image = UIImage.init(named: item.icon)
            stack.addArrangedSubview(itemView)
            itemView.onClick = { [weak self] in
                guard let self = self else {return}
                if let onClick = self.onClick {
                    onClick(item)
                }
            }
        }
        stack.distribution = .fillEqually
        addSubview(stack)
    }
    
    func addConstraints() {
        stack.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    //MARK: --------------------delegate--------------------
    
    

}

extension MineItemView {
    class ItemView : UIView {
        var onClick : (()->())?
        
        let title = ZMUtil.label(title: "商城订单", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 12, weight: .medium))
        
        let icon = ZMUtil.imageView(imageName: "right_red")

        
        override init(frame: CGRect) {
            super.init(frame: frame)
            setUI()
        }
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            setUI()
        }
        
        func setUI() {
            let t = UITapGestureRecognizer(target: self, action: #selector(tap))
            addGestureRecognizer(t)
            addSubview(title)
            addSubview(icon)
            backgroundColor = .white
            layer.cornerRadius = 8
            title.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(8)
                make.top.equalToSuperview().offset(8)
            }
            
            icon.snp.makeConstraints { make in
                make.height.width.equalTo(24)
                make.right.equalToSuperview().offset(-4)
                make.bottom.equalToSuperview().offset(-4)
                make.top.equalTo(title.snp.bottom)
            }
            
        }
        
        @objc func tap() {
            if let onClick = onClick {
                onClick()
            }
        }
    }
}
